/**
 * 页面加载时触发的事件
 */
window.onload = function onLoginLoaded() {
	GetLastUser();
};

/**
 * 监听回车键
 */
document.onkeydown = function(event) {
	var form = document.forms['loginForm'];
	event = event ? event : window.event ;
    if(event.keyCode == 13) { 
    	if(form.email.value!="" 
    		&& form.password.value!=""
    		&& form.validate.value!=""){
    		login();
    	}
    } 
}; 

/**
 * 登录
 */
function login(){
	var form = document.forms['loginForm'];
	if(form.email.value==""){
		alert("账号不能为空！");
	}else if(form.password.value==""){
		alert("密码不能为空！");
	}else if(form.validate.value==""){
		alert("请输入验证码！");
	}else{
		form.submit();
	}
	SetPwdAndChk(); //触发客户端事件
}

/**
 * 获取最后登录的用户
 */
function GetLastUser() {
	var last = "lastUser";// GUID标识符
	var usr = GetCookie(last);
	if (usr != null) {
		document.getElementById('email').value = usr;
	} else {
		document.getElementById('email').value = "";
	}
	GetPwdAndChk();
}

/**
 * 点击登录时触发客户端事件
 */ 
function SetPwdAndChk() {
	var usr = document.getElementById('email').value; //取用户名
	if(document.getElementById('chkRememberPwd').checked == true) { // 如果记住密码选项被选中
		SetLastUser(usr); //将最后一个用户信息写入到Cookie
		var pwd = document.getElementById('password').value; // 取密码值
		var expdate = new Date();
		expdate.setTime(expdate.getTime() + 7 * (24 * 60 * 60 * 1000));
		SetCookie(usr, pwd, expdate); // 将用户名和密码写入到Cookie
	}else{ // 如果没有选中记住密码,则立即过期
		ResetCookie();
	}
}

/**
 * 设置最后登录的用户
 */
function SetLastUser(usr) {
	var last = "lastUser";
	var expdate = new Date();
	expdate.setTime(expdate.getTime() + 7 * (24 * 60 * 60 * 1000)); // 当前时间加上一周的时间
	SetCookie(last, usr, expdate);
}

/**
 * 用户名失去焦点时调用该方法
 */
function GetPwdAndChk() {
	var usr = document.getElementById('email').value;
	var pwd = GetCookie(usr);
	if (pwd != null) {
		document.getElementById('chkRememberPwd').checked = true;
		document.getElementById('password').value = pwd;
	}else{
		document.getElementById('chkRememberPwd').checked = false;
		document.getElementById('password').value = "";
	}
}

/**
 * 取Cookie的值
 */
function GetCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal(j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0)
			break;
	}
	return null;
}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf(";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

/**
 * 写入到Cookie
 */
function SetCookie(name, value, expires) {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length; // 本例中length = 3
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape(value)
			+ ((expires == null) ? "" : ("; expires=" + expires.toGMTString()))
			+ ((path == null) ? "" : ("; path=" + path))
			+ ((domain == null) ? "" : ("; domain=" + domain))
			+ ((secure == true) ? "; secure" : "");
}

/**
 * 重置cookies
 */
function ResetCookie() {
	var usr = document.getElementById('email').value;
	var expdate = new Date();
	SetCookie(usr, null, expdate);
}

/**
 * 重新获取验证图片
 */
function reloadImage() {
	var verify=document.getElementById('image');
	    verify.setAttribute('src','/daycodefront/jpaas_jsp/image.jsp?it='+Math.random());
}

/**
 * 不显示错误
 */
function ShowCorrect(str) {
	document.getElementById("tip_server_validate").style.display="none";
}

